﻿<?php

/**
 * t-blog 数据库操作类
 * $Author: share112 $
 * $Id: mysql.class.php 2010-09-14 21:18:35Z  share112$
 */
 
 if (!defined('IN_TBLOG')){
    die('Hacking attempt');
 }
 
 class Mysql{
 
    var $hostaddress = "localhost";
	var $username    = "";
	var $password    = "";
	var $database    = "";
	var $mysql_port  = "3306";
	
	/**
	*
	* 初始化函数 连接数据库
	*/
	public function __construct(){
	
	 //do some thing;
	}
 
    
	/**
	*
	* 向数据库中插入数据
	*
	*/
    public function db_insert($tablename = "", $arr_cloums = ""){
	 
	   $sql_str  = "insert into $tablename set ";
	   $sql_str .= $this->mk_sql($arr_cloums); 
	   return $this->db_query($sql_str);
	}
	
	/**
	*
	* 更新数据库中数据
	*
	*/
	public function db_update($tablename = "", $arr_cloums = "",$limit_arr = ""){
	
	   $sql_str = "update $tablename set ";
	   $sql_str .= $this->mk_sql($arr_cloums);
	   $sql_str .= " where 1 ";
	   $sql_str .= $this->sql_limit($limit_data);
	   return $this->db_query($sql_str);
	}
	
	/**
	*
	* 执行查询
	*/
	private function db_query($sql_str){
	  return @mysql_query($sql_str);
	}
	
	/**
	*
	* 生成插入的字符串
	*/
	private function mk_sql($arr_cloums){
	
	   if(is_array($arr_cloums) && empty($arr_cloums)!=FALSE){
	     foreach($arr_cloums as $key=>$value){
		   $arr_str[] = "$key='$value'";   
		 }
		 $sql_str .= implode(",",$arr_str);
	   }else{
	     $sql_str = "";
	   }
	 
	   return $sql_str; 
	}
 
   /**
   *
   *  生成限制条件的字符串
   *
   */
   private function sql_limit($limit_data){
      if(is_array($limit_data)){
	     foreach($limit_data as $key=>$value){
		   $sql_str.=" and $key='$value'";   
		 }
	   }else{
	       $sql_str = "";
	   }
	   return $sql_str;
   }
   
   
   /**
    * 
    * 析構函數
    * 
    */
   public function __destruct(){
	
   }
   
   
   
    
 }
 

 
 
 
?>